package com.example.ccj.b.c.dialogfragment;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;

import com.example.ccj.u.ActivityUtils;
import com.example.ccj.u.LogUtils;
import com.example.ccj.u.ToastUtils;

/**
 * DialogFragment基类
 * @author 驰珺
 * whechat   : chijun9
 * create at : 2024/12/9 20:19
 */
public abstract class BaseDialogFragment extends DialogFragment {


    private int DEFAULT_WIDTH = WindowManager.LayoutParams.MATCH_PARENT;//宽
    private int DEFAULT_HEIGHT = WindowManager.LayoutParams.WRAP_CONTENT;//高
    private int DEFAULT_GRAVITY = Gravity.BOTTOM;//位置

    private boolean mCancelable = true;//默认可取消
    private boolean mCanceledOnTouchOutside = true;//默认点击外部可取消

    private Context mContext;

    public BaseDialogFragment(Context context) {
        this.mContext = context;
    }



    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if (getActivity() != null) {
            View mView = getActivity().getLayoutInflater().inflate(getLayoutId(), container, false);
            initViews(mView);
            initData();
            return mView;
        } else {
            return inflater.inflate(getLayoutId(), container, false);
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        Dialog mDialog = super.onCreateDialog(savedInstanceState);
        //初始化
        mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);  //去除标题
        mDialog.setCanceledOnTouchOutside(mCanceledOnTouchOutside);
        mDialog.setCancelable(mCancelable);
        Window window = mDialog.getWindow();
        if (null != window) {
            window.getDecorView().setPadding(0, 0, 0, 0);
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = DEFAULT_WIDTH;
            lp.height = DEFAULT_HEIGHT;
            lp.gravity = DEFAULT_GRAVITY;
            lp.windowAnimations = android.R.style.Animation_InputMethod;
            window.setAttributes(lp);
        }
        mDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                return !mCancelable;
            }
        });
        return mDialog;
    }

    /**
     * 设置位置
     * @param gravity
     */
    public void setGravity(int gravity) {
        DEFAULT_GRAVITY = gravity;
    }

    /**
     * 设置宽
     * @param width
     */
    public void setWidth(int width) {
        DEFAULT_WIDTH = width;
    }

    /**
     * 设置高
     * @param height
     */
    public void setHeight(int height) {
        DEFAULT_HEIGHT = height;
    }

    /**
     * 设置点击返回按钮是否可取消
     *
     * @param cancelable
     */
    public void setCancelable(boolean cancelable) {
        mCancelable = cancelable;
    }

    /**
     * 设置点击外部是否可取消
     *
     * @param canceledOnTouchOutside
     */
    public void setCanceledOnTouchOutside(boolean canceledOnTouchOutside) {
        mCanceledOnTouchOutside = canceledOnTouchOutside;
    }

    /**
     * 设置布局
     *
     * @return
     */
    protected abstract int getLayoutId();

    /**
     * 初始化Views
     *
     * @param v
     */
    protected abstract void initViews(View v);
    protected abstract void initData();

    public void show(){
        if (mContext != null && mContext instanceof FragmentActivity) {
            show(((FragmentActivity)mContext).getSupportFragmentManager(),"");
        } else {
            throw new RuntimeException("打开dialogFragment失败，必须由FragmentActivity调用");
        }
    }


    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
    }


}
